package com.hyg.lib_music.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_common.DataModel.EventBusMessage;
import com.hyg.lib_common.DataModel.YangShengInfo.ReceiveDataInt;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_music.R;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author: Administrator
 * @date: 2021/4/21
 */
public class MusicCheckBox extends LinearLayout {
    Context context;
    int id=-1;
    CheckBox checkBox;
    public MusicCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View view=LayoutInflater.from(context).inflate(R.layout.view_checkbox_ln, this);
        checkBox=view.findViewById(R.id.cb_fav);
    }

    public void setData(int id) {
        this.id = id;
    }

    public void setCHecked(int id,boolean ischecked){
        checkBox.setChecked(ischecked);
        this.id=id;
    }


    public int getMusicId() {
        return id;
    }

    public void setCHecked(boolean ischecked){
        checkBox.setChecked(ischecked);
    }

    public void initCheckbox() {
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(id==-1)return;
                if(isChecked){
                    ArrayList<Integer> list=new ArrayList<>();
                    list.add(id);
                    CommonHttpUtil.addMultilFavList(context, list.toString(), new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            ReceiveDataInt dataInt= JSONObject.parseObject(response.body().string(),ReceiveDataInt.class);
                            Log.i("lgb", "onResponse: Add_Favorite"+dataInt.getMessage());
                            if(dataInt.getCode()==200){
                                EventBus.getDefault().post(new EventBusMessage(Constants.Add_Favorite,id));
                            }
                        }
                    });
                }else {
                    ArrayList<Integer> list=new ArrayList<>();
                    list.add(id);
                    CommonHttpUtil.removeMultilFavList(context, list.toString(), new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            ReceiveDataInt dataInt= JSONObject.parseObject(response.body().string(),ReceiveDataInt.class);
                            Log.i("lgb", "onResponse: Add_Favorite"+dataInt.getMessage());
                            if(dataInt.getCode()==200){
                                EventBus.getDefault().post(new EventBusMessage(Constants.Cancel_Favorite,id));

                            }
                        }
                    });
                }
            }
        });
    }

}
